<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/24
 * Time: 13:21
 */
class OfficialCarMsgtpl extends BaseMsgtpl
{
    /**
     * 给教师发消息
     */
    public function sendMessageToTeacher($order_id,$accountId)
    {
        $score = M('OfficialCarlog')->where(array('order_id'=>$order_id,'status'=>6))->getField('score');
        $order = M('OfficialOrder')->where(array('order_id'=>$order_id))->find();

        $condition = array(
            'user_id' => $order['user_id'],
        );

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');

        $conf_id = D('OfficialConfig')->where(array('account_id' => $accountId))->field('conf_id')->find();
        $url = C('site_url')."/base/officialCar/tOrderDetail/order_id/".$order['order_id'].'/conf_id/'.$conf_id['conf_id'];

        if($wechatUser['subscribe']) {
            if($accountId == 1){//测试模板
                $this->wxTplSign = 'djC5itFa7I_2EQfxAqLZ8TGFJgDbNJ1rkSkv-MJ-cOU';
            }elseif($accountId == 2) { //滨江后勤
                $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
            }elseif($accountId == 5) {  //农林
                $this->wxTplSign = 'Kue1a_J0W5a9Mol2oZjVQwH_jWP-V_zjIrV9mA1VGOM';
            }elseif($accountId == 6) {  //暨阳学院
                $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
            }elseif($accountId == 28){
                $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            }elseif($accountId == 60){
                $this->wxTplSign = 'b927Utg2VSH5U2K1ivojHAGacjyUUt_s-i9GzjRTTzY';
            }
            if($accountId == 60){
                $this->wechatInfo['appid'] = 'wxc8142f36a90232b8';
                $this->wechatInfo['appsecret'] = '8accfa5384286e98f515a4e2b0e8682a';
                if($order['status'] == 3){
                    $info = array(
                        'first' => array("value" => '订单审核未通过'),
                        'keyword1' => array('value'=>$order['user_location']),
                        'keyword2' => array("value" => $order['user_destinction']),
                        'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'keyword4' => array("value" => ''),
                        'keyword5' => array("value" => ''),
                        'remark' => array("value" => "点击查看详情。"),
                    );
                }else{
                    $info = array(
                        'first' => array("value" => '订单已审核，查看详情'),
                        'keyword1' => array('value'=>$order['user_location']),
                        'keyword2' => array("value" => $order['user_destinction']),
                        'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'keyword4' => array("value" => ''),
                        'keyword5' => array("value" => ''),
                        'remark' => array("value" => "点击查看详情。"),
                    );
                }

            }else{
                //组消息内容
                if($order['status'] == 1){
                    // 司机完成接送
                    $info=array(
                        'first' =>array('value'=>'你已下单成功'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }elseif($order['status'] == 2){
                    //司机开始接送
                    $info=array(
                        'first' =>array('value'=>'订单审核通过'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                } elseif($order['status'] == 3){
                    //司机开始接送
                    $info=array(
                        'first' =>array('value'=>'订单审核未通过'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                } elseif($order['status'] == 4){
                    //司机开始接送
                    $info=array(
                        'first' =>array('value'=>'司机已接单，请耐心等待'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }elseif($order['status'] == 5){
                    // 司机完成接送
                    $info=array(
                        'first' =>array('value'=>'驾驶员已接送完成，赶快去评价吧~'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }elseif($order['status'] == 6){
                    // 用户评价定单
                    if($score == 1 || $score == 3){
                        $info=array(
                            'first' =>array('value'=>'你已评价完成'),
                            'keyword1' =>array('value'=>$order['user_location']),
                            'keyword2' =>array('value'=>$order['user_destinction']),
                            'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                            'remark' =>array('value'=>'感谢您对用车服务部的关注与支持，欢迎您下次用车！'),
                        );
                    }elseif($score == 2){
                        $info=array(
                            'first' =>array('value'=>'你已评价完成'),
                            'keyword1' =>array('value'=>$order['user_location']),
                            'keyword2' =>array('value'=>$order['user_destinction']),
                            'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                            'remark' =>array('value'=>'感谢您对用车服务部的关注与支持，对该次服务经核实后我们会对相关人员进行相应的处罚！'),
                        );
                    }
                }elseif($order['status'] == 8){
                    // 司机完成接送
                    $info=array(
                        'first' =>array('value'=>'订单已经审核通过~'),
                        'keyword1' =>array('value'=>$order['user_location']),
                        'keyword2' =>array('value'=>$order['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$order['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }
            }

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    /**
     * 给调度员发消息
     */
    public function sendMessageToAdmin($order_id,$accountId)
    {
        $orderInfo = M('OfficialOrder')->where(array('order_id'=>$order_id))->find();

        $admin = M('OfficialAdmin')->where(array('account_id'=>$accountId,'cut_off'=>0))->select();

        $conf_id = D('OfficialConfig')->where(array('account_id' => $accountId))->field('conf_id')->find();

        $url = C('site_url')."/base/officialCar/index/conf_id/".$conf_id['conf_id'];
        foreach($admin as $key => $value){
            $condition = array(
                'wechat_user_id' => $admin[$key]['wechat_user_id'],
            );
            //查询微信用户信息
            $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');
            if($wechatUser['subscribe']) {
                if($accountId == 1){
                    $this->wxTplSign = 'djC5itFa7I_2EQfxAqLZ8TGFJgDbNJ1rkSkv-MJ-cOU';
                }elseif($accountId == 2) { //滨江后勤
                    $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
                }elseif($accountId == 5) {  //农林
                    $this->wxTplSign = 'Kue1a_J0W5a9Mol2oZjVQwH_jWP-V_zjIrV9mA1VGOM';
                }elseif($accountId == 6) {  //暨阳学院
                    $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
                }elseif($accountId == 28){
                    $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
                }elseif($accountId == 60){
                    $this->wxTplSign = 'b927Utg2VSH5U2K1ivojHAGacjyUUt_s-i9GzjRTTzY';
                }

                if($orderInfo['status'] == 1){
                    if($accountId == 60){
                        $this->wechatInfo['appid'] = 'wxc8142f36a90232b8';
                        $this->wechatInfo['appsecret'] = '8accfa5384286e98f515a4e2b0e8682a';
                        $info = array(
                            'first' => array("value" => '你有一条未审核订单，请及时审核！'),
                            'keyword1' => array('value'=>$orderInfo['user_location']),
                            'keyword2' => array("value" => $orderInfo['user_destinction']),
                            'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                            'keyword4' => array("value" => ''),
                            'keyword5' => array("value" => ''),
                            'remark' => array("value" => "点击查看详情。"),
                        );
                    }else{
                        //司机开始接送
                        $info=array(
                            'first' => array('value' => '你有一条未审核订单，请及时审核！'),
                            'keyword1' =>array('value'=>$orderInfo['user_location']),
                            'keyword2' =>array('value'=>$orderInfo['user_destinction']),
                            'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                            'remark' =>array('value'=>''),
                        );
                    }
                }
                $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
            }
        }

        return true;
    }

    /**
     * 给司机发消息
     */
    public function sendMessageToDriver($order_id,$driver_id,$accountId)
    {

        $orderInfo = M('OfficialOrder')->where(array('order_id'=>$order_id))->find();

        $driver = M('OfficialDriver')->where(array('driver_id'=>$driver_id))->find();

        $condition = array(
            'wechat_user_id' => $driver['wechat_user_id'],
        );

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');

        $conf_id = D('OfficialConfig')->where(array('account_id' => $accountId))->field('conf_id')->find();

        $url = C('site_url')."/base/officialCar/detail/order_id/".$orderInfo['order_id'].'/conf_id/'.$conf_id['conf_id'];
//        $url = '';
        if($wechatUser['subscribe']) {
            if($accountId == 1){
                $this->wxTplSign = 'djC5itFa7I_2EQfxAqLZ8TGFJgDbNJ1rkSkv-MJ-cOU';
            }elseif($accountId == 2) { //滨江后勤
                $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
            }elseif($accountId == 5) {  //农林
                $this->wxTplSign = 'Kue1a_J0W5a9Mol2oZjVQwH_jWP-V_zjIrV9mA1VGOM';
            }elseif($accountId == 6) {  //暨阳学院
                $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
            }elseif($accountId == 28){
                $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            }elseif($accountId == 60){
                $this->wxTplSign = 'b927Utg2VSH5U2K1ivojHAGacjyUUt_s-i9GzjRTTzY';
            }

            if($orderInfo['status'] == 6){
                if($accountId == 60){
                    $this->wechatInfo['appid'] = 'wxc8142f36a90232b8';
                    $this->wechatInfo['appsecret'] = '8accfa5384286e98f515a4e2b0e8682a';
                    $info = array(
                        'first' => array("value" => '教师已评价，快去查看吧！'),
                        'keyword1' => array('value'=>$orderInfo['user_location']),
                        'keyword2' => array("value" => $orderInfo['user_destinction']),
                        'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                        'keyword4' => array("value" => ''),
                        'keyword5' => array("value" => ''),
                        'remark' => array("value" => "点击查看详情。"),
                    );
                }else{
                    //司机开始接送
                    $info=array(
                        'first' =>array('value' => '教师已评价，快去查看吧！'),
                        'keyword1' =>array('value'=>$orderInfo['user_location']),
                        'keyword2' =>array('value'=>$orderInfo['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }
            }elseif($orderInfo['status'] == 2){
                if($accountId == 60){
                    $this->wechatInfo['appid'] = 'wxc8142f36a90232b8';
                    $this->wechatInfo['appsecret'] = '8accfa5384286e98f515a4e2b0e8682a';
                    $info = array(
                        'first' => array("value" => '你有一条新订单'.'/'.'学院部门-'.$orderInfo['user_college']),
                        'keyword1' => array('value'=>$orderInfo['user_location']),
                        'keyword2' => array("value" => $orderInfo['user_destinction']),
                        'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                        'keyword4' => array("value" => ''),
                        'keyword5' => array("value" => ''),
                        'remark' => array("value" => "点击查看详情。"),
                    );
                }else{
                    //司机开始接送
                    $info=array(
                        'first' =>array('value' => '你有一条新订单'.'/'.'学院部门-'.$orderInfo['user_college']),
                        'keyword1' =>array('value'=>$orderInfo['user_location']), //出发地点
                        'keyword2' =>array('value'=>$orderInfo['user_destinction']), //目的地
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])), //开始时间
                        'remark' =>array('value'=>''),
                    );
                }
            }

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    /**
     * 给评价通知人员发消息
     */
    public function sendMessageToNotice($wechatUserId,$order_id,$accountId)
    {
        $orderInfo = M('OfficialOrder')->where(array('order_id'=>$order_id))->find();
        $wechatUser = M('WechatUser')->where(array('wechat_user_id'=>$wechatUserId,'account_id'=>$accountId,'subscribe'=>1))->field('openid,subscribe')->find();

        $conf_id = D('OfficialConfig')->where(array('account_id' => $accountId))->field('conf_id')->find();
        $driver_id = D('OfficialCarlog')->where(array('order_id' => $order_id,'status' => 6))->field('driver_id')->find();
        $url = C('site_url')."/base/officialCar/detail/order_id/".$orderInfo['order_id'].'/driver_id/'.$driver_id['driver_id'].'/conf_id/'.$conf_id['conf_id'];
        if($wechatUser['subscribe']) {
            if($accountId == 1){
                $this->wxTplSign = 'djC5itFa7I_2EQfxAqLZ8TGFJgDbNJ1rkSkv-MJ-cOU';
            }elseif($accountId == 2) { //滨江后勤
                $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
            }elseif($accountId == 5) {  //农林
                $this->wxTplSign = 'Kue1a_J0W5a9Mol2oZjVQwH_jWP-V_zjIrV9mA1VGOM';
            }elseif($accountId == 6) {  //暨阳学院
                $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
            }elseif($accountId == 28){
                $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
            }elseif($accountId == 60){
                $this->wxTplSign = 'b927Utg2VSH5U2K1ivojHAGacjyUUt_s-i9GzjRTTzY';
            }

            if($orderInfo['status'] == 6){
                if($accountId == 60){
                    $this->wechatInfo['appid'] = 'wxc8142f36a90232b8';
                    $this->wechatInfo['appsecret'] = '8accfa5384286e98f515a4e2b0e8682a';
                    $info = array(
                        'first' => array("value" => '教师已评价，快去查看吧！'),
                        'keyword1' => array('value'=>$orderInfo['user_location']),
                        'keyword2' => array("value" => $orderInfo['user_destinction']),
                        'keyword3' => array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                        'keyword4' => array("value" => ''),
                        'keyword5' => array("value" => ''),
                        'remark' => array("value" => "点击查看详情。"),
                    );
                }else{
                    //司机开始接送
                    $info=array(
                        'first' =>array('value' => '教师已评价，快去查看吧！'),
                        'keyword1' =>array('value'=>$orderInfo['user_location']),
                        'keyword2' =>array('value'=>$orderInfo['user_destinction']),
                        'keyword3' =>array('value'=>date('Y年m月d日 H:i:s',$orderInfo['car_start_time'])),
                        'remark' =>array('value'=>''),
                    );
                }
            }

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

}